using System;
using System.Data;

using WP.Commons;
using WP.DB;
using WP.MF.DataEntity; 

namespace WP.MF.DataAccess
{
    public class MMiscSwitchDAL : IDataAccess
    {
        public MMiscSwitchDAL()
		{
			//
			// TODO: Add constructor logic here
			//
		}

        #region Select List
        public string SQLList(int iParameter)
        {
            string strSQL = @"SELECT RecID,SystemName,SwitchNo,SwitchName,Description,SwitchValue,ProgressSwitchName,NotUsed 
                             FROM MMiscSwitch where 1=1 " + Permission.GetSubCondition(false, "mmiscSwitch", iParameter) +
                            " order By SwitchNo,SwitchName";
            return strSQL;
        }
        #endregion

        #region Fields
        public string Fields
        {
            get
            {
                return @"SwitchNo,SwitchName,Description,SwitchValue,ProgressSwitchName,Type,MaxLength,SystemName,
                         FormatString,CheckString,SpecialSwitch,InSwitchMaint,NotUsed";

            }
        }
        #endregion

        #region Insert MMiscSwitch by DataRow
        public void InsertMMiscSwitch(DBAccess dBAccess, MMiscSwitchDEL mMiscSwitchDEL)
        {
            string strSql = string.Empty;
            DataRow dr = mMiscSwitchDEL.Tables[0].Rows[0];

            strSql = BaseFunctions.GenerateSqlForDataAccess(Fields, MMiscSwitchDEL.TABLENAME_MMISCSWITCH, dr, FormStatus.AddNew);

            dr[MMiscSwitchDEL.RECID] = dBAccess.GetObjectValue(strSql);
            strSql = @"INSERT INTO mswitchLog  (UserName,Udate,UTime,USys,Logical,SwitchName,NewValue) 
                            VALUES ( '{0}','{1}','{2}','{3}','{4}','{5}','{6}' )";
            strSql = string.Format(strSql, SysVar.UserName, 
                                           DateTime.Now.Date.ToString(),
                                           string.Format("{0:00}", DateTime.Now.Hour.ToString()) + ":" + string.Format("{0:00}", DateTime.Now.Minute.ToString()),
                                           dr[MMiscSwitchDEL.SYSTEMNAME].ToString(),
                                           0,
                                           dr[MMiscSwitchDEL.SWITCHNAME].ToString(),
                                           dr[MMiscSwitchDEL.SWITCHVALUE].ToString()
                                           );
            dBAccess.ExecuteSql(strSql);
        }
        #endregion

        #region Update MMiscSwitch by DataRow
        public void UpdateMMiscSwitch(DBAccess dBAccess, MMiscSwitchDEL mMiscSwitchDEL)
        {
            string strSql = string.Empty;
           
            DataRow dr = mMiscSwitchDEL.Tables[0].Rows[0];
            string strOldValues = DataFunctions.GetFieldValue(dBAccess, MMiscSwitchDEL.TABLENAME_MMISCSWITCH, MMiscSwitchDEL.SWITCHVALUE, "RecId =" + dr[MMiscSwitchDEL.RECID]);

            strSql = BaseFunctions.GenerateSqlForDataAccess(Fields, MMiscSwitchDEL.TABLENAME_MMISCSWITCH, dr, FormStatus.Update);

            dr[MMiscSwitchDEL.RECSTAMP] = dBAccess.GetObjectValue(strSql);

            strSql = @"INSERT INTO mswitchLog  (UserName,Udate,UTime,USys,Logical,SwitchName,OriginValue,NewValue) 
                            VALUES ( '{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}' )";
            strSql = string.Format(strSql, SysVar.UserName,
                                           DateTime.Now.Date.ToString(),
                                           string.Format("{0:00}", DateTime.Now.Hour.ToString()) + ":" + string.Format("{0:00}", DateTime.Now.Minute.ToString()),
                                           dr[MMiscSwitchDEL.SYSTEMNAME].ToString(),
                                           0,
                                           dr[MMiscSwitchDEL.SWITCHNAME].ToString(),
                                           strOldValues,
                                           dr[MMiscSwitchDEL.SWITCHVALUE].ToString()
                                           );
            dBAccess.ExecuteSql(strSql);
        }
        #endregion

        #region Delete MMiscSwitch by Key
        public void DeleteMMiscSwitch(DBAccess dBAccess, int iRecID)
        {
            string strSql = string.Empty;

            strSql = @"DELETE FROM mMiscSwitch WHERE RecID=" + iRecID;

            dBAccess.ExecuteSql(strSql);
        }
        #endregion
        #region Update MSwitchRemark
        public void UpdateMSwitchRemark(DBAccess dbAccess,string strModuleName,string strSwitchName,string strLogic,string strRemark)
        {
            string strSql = string.Empty;
            strSql = "insert into mSwitchRemark (ModuleName,SwitchName,Logic,Remark) Values ('" + strModuleName + "','" + strSwitchName + "'," + (strLogic.ToUpper() =="YES"? 1 : 0) + ",'" + strRemark + "')";
            dbAccess.ExecuteSql(strSql);
        }
        #endregion
        #region Delete MSwitchRemark
        public void DeleteMSwitchRemark(DBAccess dbAccess, string strModuleName, string strSwitchName)
        {
            string strSql = string.Empty;
            strSql = "delete from mSwitchRemark where ModuleName='" + strModuleName+"' and SwitchName='"+strSwitchName+"'";
            dbAccess.ExecuteSql(strSql);
        }
        #endregion
    }
}
